/**
 * Filename: NativeStickyGridAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/10 0:37
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.NativePreviewActivity;
import com.github.postsanf.yinian.bean.YNNativeImage;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.nativestick.StickyGridHeadersSimpleAdapter;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.HashMap;
import java.util.List;

public class LocalStickyImportPregressGridAdapter extends BaseAdapter implements
        StickyGridHeadersSimpleAdapter {

    private boolean isSelectStatus;     // 是否在选择状态
    private List<YNNativeImage> hasHeaderIdList;
    private LayoutInflater mInflater;
    private Context mContext;
    private Resources mResource;
    private HashMap<String, View> visibale = new HashMap<>();

    public void setResource(Resources mResource) {
        this.mResource = mResource;
    }


    public LocalStickyImportPregressGridAdapter(Context mContext, List<YNNativeImage> hasHeaderIdList, boolean isSelectStatus) {
        mInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.hasHeaderIdList = hasHeaderIdList;
        this.isSelectStatus = isSelectStatus;
        if (YNSingleton.getInstance().getNativePhoto().size() == 0 || YNSingleton.getInstance().getNativePhoto() != hasHeaderIdList) {
            YNSingleton.getInstance().setNativePhoto(hasHeaderIdList);
        }
//        registerReceiver();
    }

    public void setSelectStatus(boolean isSelectStatus) {
        this.isSelectStatus = isSelectStatus;
    }

    @Override
    public int getCount() {
        return hasHeaderIdList.size();
    }

    @Override
    public Object getItem(int position) {
        return hasHeaderIdList;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder mViewHolder;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.yn_item_native_image, parent, false);
            mViewHolder.mImageView = (SimpleDraweeView) convertView.findViewById(R.id.native_item_image);
            mViewHolder.mNative_item_select = (ImageView) convertView.findViewById(R.id.native_item_select);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        final String path = hasHeaderIdList.get(position).getThumbnail();
        mViewHolder.mImageView.setTag(path);
        mViewHolder.mImageView.setImageResource(R.drawable.pic_null);
        if (mResource != null) {
            mViewHolder.mNative_item_select.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("picture_unselected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
        } else {
            mViewHolder.mNative_item_select.setImageResource(R.drawable.pic_import_unselected);
        }
        Uri uri = Uri.parse(path);
        ImageOptHelper.showFrescoNative(uri, mViewHolder.mImageView, mContext);
        // 设置图片宽高
        final int screenWidth = DisplayUtils.getScreenWidthPixels((Activity) mContext);
        int horizontalSpacing = DisplayUtils.dp2px(mContext, 2);
        int itemWidth = (screenWidth - 2 * horizontalSpacing) / 3;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
        mViewHolder.mImageView.setLayoutParams(params);
        if (isSelectStatus) {
            mViewHolder.mNative_item_select.setVisibility(View.VISIBLE);
            mViewHolder.mImageView.setColorFilter(null);
            // 图片选择事件
            mViewHolder.mImageView.setOnClickListener(new View.OnClickListener() {
                //选择，则将图片变暗，反之则反之
                @Override
                public void onClick(View v) {
                    // 已经选择过该图片
                    if (SelectedImg.selectedNetPic.size() != 0 && SelectedImg.selectedNetPic.contains(hasHeaderIdList.get(position))) {
                        SelectedImg.selectedNetPic.remove(hasHeaderIdList.get(position));
                        if (mResource != null) {
                            mViewHolder.mNative_item_select.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("picture_unselected", CommonConstants.THEME_COLOR, CommonConstants.THEME_PACKAGE)));
                        } else {
                            mViewHolder.mNative_item_select.setImageResource(R.drawable.pic_import_unselected);
                        }
                        mViewHolder.mImageView.setColorFilter(null);
                        ScaleAnimation scaleAnimation = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                        scaleAnimation.setFillAfter(true);
                        mViewHolder.mImageView.setAnimation(scaleAnimation);
                        scaleAnimation.startNow();
                    } else
                    // 未选择该图片
                    {
                        SelectedImg.selectedNetPic.add(hasHeaderIdList.get(position));
                        if (mResource != null) {
                            mViewHolder.mNative_item_select.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("pictures_selected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
                        } else {
                            mViewHolder.mNative_item_select.setImageResource(R.drawable.pic_import_selected);
                        }
                        mViewHolder.mImageView.setColorFilter(Color.parseColor("#33000000"));
                        ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.9f, 1.0f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                        scaleAnimation.setFillAfter(true);
                        mViewHolder.mImageView.setAnimation(scaleAnimation);
                        scaleAnimation.startNow();
                    }
                }
            });

            mViewHolder.mImageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return false;
                }
            });
        } else {
            mViewHolder.mNative_item_select.setVisibility(View.GONE);
            mViewHolder.mImageView.setColorFilter(null);
        }
        // 响应选择效果
        if (SelectedImg.selectedNetPic.contains(hasHeaderIdList.get(position))) {
            if (mResource != null) {
                mViewHolder.mNative_item_select.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("pictures_selected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
            } else {
                mViewHolder.mNative_item_select.setImageResource(R.drawable.pic_import_selected);
            }
            mViewHolder.mImageView.setColorFilter(Color.parseColor("#33000000"));
            ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.9f, 1.0f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            scaleAnimation.setFillAfter(true);
            mViewHolder.mImageView.setAnimation(scaleAnimation);
            scaleAnimation.startNow();
        } else {
            if (mResource != null) {
                mViewHolder.mNative_item_select.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("pictures_selected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
            } else {
                mViewHolder.mNative_item_select.setImageResource(R.drawable.pic_import_unselected);
            }
            mViewHolder.mImageView.setColorFilter(null);
            ScaleAnimation scaleAnimation = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            scaleAnimation.setFillAfter(true);
            mViewHolder.mImageView.setAnimation(scaleAnimation);
            scaleAnimation.startNow();
        }
        if (!isSelectStatus) {
            mViewHolder.mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    YNSingleton.getInstance().setNativePhoto(hasHeaderIdList);
                    Intent intent = new Intent(mContext, NativePreviewActivity.class);
                    intent.putExtra(CommonConstants.YNDATAS, position);
                    mContext.startActivity(intent);
                }
            });
        }
        return convertView;
    }

    @Override
    public View getHeaderView(final int position, View convertView, ViewGroup parent) {
        final HeaderViewHolder mHeaderHolder;
        if (convertView == null) {
            mHeaderHolder = new HeaderViewHolder();
            convertView = mInflater.inflate(R.layout.yn_item_local_image_header, parent, false);
            mHeaderHolder.mTextView = (TextView) convertView.findViewById(R.id.native_header_time);
            mHeaderHolder.mimageButton = ((ImageView) convertView.findViewById(R.id.all_pic_select));
            mHeaderHolder.ll_head = ((LinearLayout) convertView.findViewById(R.id.ll_head));
            convertView.setTag(mHeaderHolder);
        } else {
            mHeaderHolder = (HeaderViewHolder) convertView.getTag();
        }

        visibale.put(hasHeaderIdList.get(position).getTime(), mHeaderHolder.mimageButton);

        mHeaderHolder.mTextView.setText(hasHeaderIdList.get(position).getTime());
        //该位置是被点击全选还是取消全选还是没有被点击过
//        if (!click.containsKey(mHeaderHolder.mTextView.getText().toString().trim()) || !click.get(mHeaderHolder.mTextView.getText().toString().trim())) {
//            mHeaderHolder.mimageButton.setImageResource(R.drawable.all_unselected);
//        } else {
//            mHeaderHolder.mimageButton.setImageResource(R.drawable.all_selected);
//        }
//        if (isSelectStatus) {
//            mHeaderHolder.mimageButton.setVisibility(View.VISIBLE);
//            mHeaderHolder.mimageButton.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (!click.containsKey(mHeaderHolder.mTextView.getText().toString().trim()) || !click.get(mHeaderHolder.mTextView.getText().toString().trim())) {
//                        for (int i = 0; i < hasHeaderIdList.size(); i++) {
//                            if (TextUtils.equals(mHeaderHolder.mTextView.getText().toString().trim(), hasHeaderIdList.get(i).getTime())) {
//                                SelectedImg.selectedNetPic.add(hasHeaderIdList.get(i));
//                            }
//                        }
//                        click.put(mHeaderHolder.mTextView.getText().toString().trim(), true);
//                        notifyDataSetChanged();
//                        mHeaderHolder.mimageButton.setImageResource(R.drawable.all_selected);
//                    } else {
//                        ListIterator<YNNativeImage> it = SelectedImg.selectedNetPic.listIterator();
//                        while (it.hasNext()) {
//                            YNNativeImage next = it.next();
//                            if (TextUtils.equals(mHeaderHolder.mTextView.getText().toString().trim(), next.getTime())) {
//                                it.remove();
//                            }
//                        }
//                        click.put(mHeaderHolder.mTextView.getText().toString().trim(), false);
//                        notifyDataSetChanged();
//                        mHeaderHolder.mimageButton.setImageResource(R.drawable.all_unselected);
//                    }
//                }
//            });
//        } else {
//            mHeaderHolder.mimageButton.setVisibility(View.GONE);
//        }
        mHeaderHolder.mimageButton.setVisibility(View.GONE);
        return convertView;
    }

    public void update(List<YNNativeImage> data, boolean flag) {
        hasHeaderIdList = data;
        isSelectStatus = flag;
    }

    // 注册广播接收器
//    private void registerReceiver() {
//        IntentFilter filter = new IntentFilter();
//        filter.addAction(ACTIONs.clear);
//        filter.addAction(ACTIONs.invisibale);
//        filter.addAction(ACTIONs.visibale);
//        mContext.registerReceiver(mRecivier, filter);
//    }

//    public void unRegisterReceiver() {
//        mContext.unregisterReceiver(mRecivier);
//    }
//
//    // 进度更新广播接收器
//    private BroadcastReceiver mRecivier = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            switch (intent.getAction()) {
//                case ACTIONs.clear:
//                    click.clear();
//                    break;
//                case ACTIONs.invisibale:
//                    String time = intent.getStringExtra("time");
//                    Log.d("yuhao", "time=" + time);
//                    ImageView view = (ImageView) visibale.get(time);
//                    view.setVisibility(View.GONE);
//                    break;
//                case ACTIONs.visibale:
//                    String time1 = intent.getStringExtra("time");
//                    ImageView view1 = (ImageView) visibale.get(time1);
//                    view1.setVisibility(View.VISIBLE);
//                    break;
//            }
//        }
//    };


    //  获取HeaderId,HeaderId不同则New一个
    @Override
    public long getHeaderId(int position) {
        return hasHeaderIdList.get(position).getSection();
    }

    public static class ViewHolder {
        public SimpleDraweeView mImageView;
        public ImageView mNative_item_select;

        public TextView mTextView;
        public ImageView mimageButton;
        public LinearLayout ll_head;
    }

    public static class HeaderViewHolder {
        public TextView mTextView;
        public ImageView mimageButton;
        public LinearLayout ll_head;
    }
}